package com.wing.productionPlan.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.productionPlan.model.response.ProductionProcessDeviceVO;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.productionPlan.model.entity.ProductionProcessDevice;
import com.wing.productionPlan.model.request.ProductionProcessDeviceForm;
import com.wing.productionPlan.service.ProductionProcessDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.HashMap;
import java.util.Map;

/**
 * @author
 */
@Validated
@Api(value = "Admin-生产工序设备表管理", tags = "#/productionPlan/productionProcessDevice")
@RestController("productionProcessDeviceController")
@RequestMapping("productionPlan/admin/productionProcessDevice")
@RequiredArgsConstructor
public class ProductionProcessDeviceController {

    private final ProductionProcessDeviceService productionProcessDeviceService;


    @ApiOperation(value = "查询所有生产工序设备表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ProductionProcessDeviceVO> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,
            Long productionPlanId,Long processId,String deviceCode, String skuCode, String skuName, String skuSpec) {
        Page<ProductionProcessDeviceVO> pg = new Page<>(page, limit);
        QueryWrapper<ProductionProcessDevice> queryWrapper = new QueryWrapper<>();
        Map paramMap = new HashMap();
        paramMap.put("productionPlanId", productionPlanId);
        paramMap.put("processId", processId);
        paramMap.put("deviceCode", deviceCode);
        paramMap.put("skuCode", skuCode);
        paramMap.put("skuName", skuName);
        paramMap.put("skuSpec", skuSpec);
        IPage<ProductionProcessDeviceVO> list = productionProcessDeviceService.pageList4app(pg, paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加生产工序设备表", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(ProductionProcessDeviceForm.Add.class) ProductionProcessDeviceForm productionProcessDeviceForm) {
        if (productionProcessDeviceService.add(productionProcessDeviceForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }


    @ApiOperation(value = "修改生产工序设备表", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(ProductionProcessDeviceForm.Update.class) ProductionProcessDeviceForm productionProcessDeviceForm) {
        ProductionProcessDevice productionProcessDevice = productionProcessDeviceService.getById(productionProcessDeviceForm.getId());
        if (productionProcessDevice == null) {
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(ProductionProcessDevice.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(productionProcessDeviceForm, productionProcessDevice, copyOptions);
        if (productionProcessDeviceService.updateById(productionProcessDevice)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }


    @ApiOperation(value = "删除生产工序设备表", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productionProcessDeviceId", value = "生产工序设备表id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long productionProcessDeviceId) {
        if (productionProcessDeviceService.removeById(productionProcessDeviceId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
}
